package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.PrivateKey;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-11-10
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //构建查询条件

        LambdaQueryWrapper<ApUserRealname> query=new LambdaQueryWrapper<>();
        //不为空则设置查询条件
        if(dto.getStatus()!=null){
           query.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        IPage<ApUserRealname> page=new Page<>(dto.getPage(),dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        //如果没有设置状态就查全部
        PageResponseResult result=new PageResponseResult(dto.getPage(), dto.getSize(),iPage.getTotal(),iPage.getRecords());
        return result;
    }


    @Autowired
    private IApUserService userService;

    /**
     * 审核,1通过 2失败
     * @param dto
     * @param type
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        // 根据id查询
        ApUserRealname userRealname = this.getById(dto.getId());
        //判断审核是否通过,
        //不通过则返回,记录失败原因
        if(type==0){
            userRealname.setStatus(2);
            userRealname.setReason(dto.getReason());
            this.updateById(userRealname);
            return ResponseResult.okResult();
        }
        //通过
        //c查询用户注册表
        ApUser apUser = userService.getById(userRealname.getUserId());
        //根据用户注册表自动注册自媒体
        WmUser wmUser=createWmUser(apUser);
        //判断上一步是否成功
       if(wmUser!=null){
           //创建文章作者
           ApAuthor author =createAuthor(apUser,wmUser);
           if(author!=null){
               //将作者id更新到自媒体
               wmUser.setApAuthorId(author.getId());
               mediaClient.updateWmUser(wmUser);
               //修改认证状态为9
               userRealname.setStatus(9);
               this.updateById(userRealname);
               //注册表的标识
               apUser.setFlag(1);
               userService.updateById(apUser);
           }
       }
        return ResponseResult.okResult();
    }

    @Autowired
    private ArticleClient articleClient;
    /**
     * 创建文章作者
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        //构建文章作者
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        //远程调用
        ResponseResult<ApAuthor> responseResult = articleClient.saveApAuthor(author);
        //判断是否成功
        if(responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            ApAuthor data = responseResult.getData();
            return data;
        }
        //不成功则返回inull
        return null;
    }

    @Autowired
    private MediaClient mediaClient;
    /**
     * 注册自媒体
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        //创建自媒体
        WmUser wmUser=new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        //因为id也会被拷贝,设置为空
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        //通过远程调用自媒体
        ResponseResult<WmUser> responseResult = mediaClient.saveWmUser(wmUser);
        //判断是否调用成功.
        if(responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            //调用成功
            WmUser data = responseResult.getData();
            return data;
        }
        //不成功则返回inull
        return null;
    }
}
